package com.tau.egonetwork.common;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Properties;

public class Configurations {
	
	private static final String CONFIG = "config.properties";
	private static final String CONFIG_PROFILES_DIRECTORY = "ProfilesDirectory";
	private static final String CONFIG_LOGS_DIRECTORY = "LogsDirectory";
	private static final String CONFIG_DECREASE_HEIGHT = "DecreaseHeight";
	private static final String CONFIG_FACEBOOK_POPUP_WIDTH = "FacebookPopupWidth";
	private static final String CONFIG_FACEBOOK_POPUP_HEIGHT = "FacebookPopupHeight";
	private static final String CONFIG_LINKEDIN_POPUP_WIDTH = "LinkedinPopupWidth";
	private static final String CONFIG_LINKEDIN_POPUP_HEIGHT = "LinkedinPopupHeight";
	private static final String CONFIG_LINKEDIN_MAX_REQS = "LinkedinMaxContactsPerRequest";
	private static final String CONFIG_LINKEDIN_REDIRECT = "LinkedinRedirect";

	private final static String DEFAULT_PROFILE_DIR = "Profiles";
	private final static String DEFAULT_LOG_DIR = "Logs";
	private final static int DEFAULT_DECREASE_HEIGHT = 38;
	private final static int DEFAULT_FACEBOOK_POPUP_WIDTH = 550;
	private final static int DEFAULT_FACEBOOK_POPUP_HEIGHT = 700;
	private final static int DEFAULT_LINKEDIN_POPUP_WIDTH = 550;
	private final static int DEFAULT_LINKEDIN_POPUP_HEIGHT = 750;
	private final static int DEFAULT_LINKEDIN_MAX_REQS = 100;
	private final static String DEFAULT_LINKEDIN_REDIRECT = "http://www.eng.tau.ac.il/";
	
	//init configs with default values, if config file exists, override them:
	private static String profilesDirectory = DEFAULT_PROFILE_DIR;
	private static String logsDirectory = DEFAULT_LOG_DIR;
	private static int decreaseHeight = DEFAULT_DECREASE_HEIGHT;
	private static int facebookPopupWidth = DEFAULT_FACEBOOK_POPUP_WIDTH;
	private static int facebookPopupHeight = DEFAULT_FACEBOOK_POPUP_HEIGHT;
	private static int linkedinPopupWidth = DEFAULT_LINKEDIN_POPUP_WIDTH;
	private static int linkedinPopupHeight = DEFAULT_LINKEDIN_POPUP_HEIGHT;
	private static int linkedinMaxRequestsPerContact = DEFAULT_LINKEDIN_MAX_REQS;
	private static String linkedinRedirect = DEFAULT_LINKEDIN_REDIRECT;

	private Configurations() {	}
	
	static {
		
		Properties properties = new Properties();
		try {
			
			properties.load(new FileInputStream(CONFIG));
			
			profilesDirectory = properties.getProperty(
					CONFIG_PROFILES_DIRECTORY, DEFAULT_PROFILE_DIR);
			
			logsDirectory = properties.getProperty(
					CONFIG_LOGS_DIRECTORY, DEFAULT_LOG_DIR);
			
			try {
				decreaseHeight =
						Integer.parseInt(properties.getProperty(
						CONFIG_DECREASE_HEIGHT,
						DEFAULT_DECREASE_HEIGHT + ""));
			} catch (NumberFormatException e) {
				
			}
			
			try {
				facebookPopupWidth =
						Integer.parseInt(properties.getProperty(
								CONFIG_FACEBOOK_POPUP_WIDTH,
								DEFAULT_FACEBOOK_POPUP_WIDTH + ""));	
			} catch (NumberFormatException e) { }

			try {
				facebookPopupHeight =
						Integer.parseInt(properties.getProperty(
								CONFIG_FACEBOOK_POPUP_HEIGHT,
								DEFAULT_FACEBOOK_POPUP_HEIGHT + ""));	
			} catch (NumberFormatException e) { }

			try {
				linkedinPopupWidth =
						Integer.parseInt(properties.getProperty(
								CONFIG_LINKEDIN_POPUP_WIDTH,
								DEFAULT_LINKEDIN_POPUP_WIDTH + ""));	
			} catch (NumberFormatException e) { }

			try {
				linkedinPopupHeight =
						Integer.parseInt(properties.getProperty(
								CONFIG_LINKEDIN_POPUP_HEIGHT,
								DEFAULT_LINKEDIN_POPUP_HEIGHT + ""));	
			} catch (NumberFormatException e) { }
			
			try {
				linkedinMaxRequestsPerContact =
						Integer.parseInt(properties.getProperty(
								CONFIG_LINKEDIN_MAX_REQS,
								DEFAULT_LINKEDIN_MAX_REQS + ""));	
			} catch (NumberFormatException e) { }

			linkedinRedirect = properties.getProperty(
					CONFIG_LINKEDIN_REDIRECT,
					DEFAULT_LINKEDIN_REDIRECT);
			
			
		} catch (FileNotFoundException e) {
		} catch (IOException e) { }
	}
		
	public static String profilesDirectory() {
		return profilesDirectory;
	}
	
	public static String logsDirectory() {
		return logsDirectory;
	}
	
	public static int getDecreaseHeight() {
		return decreaseHeight;
	}
	
	public static int facebookPopupWidth() {
		return facebookPopupWidth;
	}

	public static int facebookPopupHeight() {
		return facebookPopupHeight;
	}

	public static int linkedinPopupWidth() {
		return linkedinPopupWidth;
	}

	public static int linkedinPopupHeight() {
		return linkedinPopupHeight;
	}
	
	public static int linkedinMaxContactPerRequest() {
		return linkedinMaxRequestsPerContact;
	}
	
	public static String getLinkedinRedirectURL() {
		return linkedinRedirect;
	}
}